﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.BackControllers.Home;
using Wwtfly.BBS.BackControllers.Attributes;
using System.Web.Mvc;
using Wwtfly.BBS.BackModels.ViewModels.Plays;
using Wwtfly.BBS.Enums.Common;
using Wwtfly.BBS.BackBLL.Plays;
using Wwtfly.Utility;
using Wwtfly.BBS.BackModels;
using Wwtfly.LoggerUtility;
using Wwtfly.BBS.BackModels.Models.Plays;
using Wwtfly.BBS.Framework.Common;
using Wwtfly.BBS.BackModels.Models.Common;

namespace Wwtfly.BBS.BackControllers.Plays
{
    /// <summary>
    /// 彩种信息控制器
    /// </summary>
    [HasFolder("Back")]
    public class BackPlaysController : BackBaseController
    {
        /// <summary>
        /// 分页大小 
        /// </summary>
        private static readonly int PageSize = 15;

        public ActionResult Index(string pageParams)
        {
            ViewData["Title"] = "彩种信息页";
            PageParams pp = !string.IsNullOrEmpty(pageParams) ? new PageParams(pageParams) :
                new PageParams() { PageIndex = 1, PageSize = PageSize };
            PlaysShowsSearchModel search = this.RestoreSearch<PlaysShowsSearchModel>(null);
            PlaysShowsViewModel viewModel = new PlaysShowsViewModel()
            {
                PageParams=pp,
                PlaysShowsPagedList = PlaysBLL.GetPlaysShowsPageList(pp, search),
                PlaysShowsSearch = search ?? new PlaysShowsSearchModel(),
                PlaysTypeList = new SelectList(PlaysTypesBLL.GetPlaysTypesList(), "LotteryID", "LotteryName")
            };
           
            return View(viewModel);
        }

        // 搜索
        [HttpPost]
        [HasPower(PowerTypeEnum.Browse)]
        public ActionResult Search(PlaysShowsViewModel viewModel)
        {
            this.SaveSearch(viewModel.PlaysShowsSearch, null);
            return RedirectToAction("Index");
        }
        //重置 搜索..
        public ActionResult SearchReset()
        {
            this.ResetSearch(null);
            return RedirectToAction("Index");
        }
        //浏览
        [HasPower(PowerTypeEnum.Browse)]
        public ActionResult Details(string id)
        {
            return RedirectToAction("Index");
        }
        // 编辑
        [HasPower(PowerTypeEnum.Edit)]
        [ValidateInput(false)]
        public ActionResult Update(string id)
        {
            ViewData["Title"] = "修改";
            return Edit(id);
        }

        [HttpPost, HasPower(PowerTypeEnum.Edit)]
        [ValidateInput(false)]
        public ActionResult Update(PlaysShowsViewModel viewModel)
        {
            return EditSave(viewModel);
        }

        [HasPower(PowerTypeEnum.Edit)]
        public ActionResult Edit(string id)
        {
            PlaysShowsViewModel viewModel = new PlaysShowsViewModel()
            {
                PlaysShows=PlaysBLL.GetInfo(id.AsInt(0))
            };
            viewModel.PlaysTypeList = new SelectList(PlaysTypesBLL.GetPlaysTypesList(), "LotteryID", "LotteryName", viewModel.PlaysShows.LotteryID.ToString());
        
            return View("Edit",viewModel);
        }
        // 添加
        [HasPower(PowerTypeEnum.Add)]
        [ValidateInput(false)]
        public ActionResult Add(string id)
        {
            ViewData["Title"] = "添加";
            return Edit(id);
        }

        // 添加
        [HttpPost, HasPower(PowerTypeEnum.Add)]
        [ValidateInput(false)]
        public ActionResult Add(PlaysShowsViewModel viewModel)
        {
            return EditSave(viewModel);

        }
        // 删除
        [HasPower(PowerTypeEnum.Delete)]
        public ActionResult Delete(string id)
        {
            try
            {
                bool b =PlaysBLL.Delete(id.AsInt(0));
                if (b)
                {
                    return this.Json(
                        new
                    JsonResultModel
                        {
                            ResultCode = "100",
                            ResultMessage = null
                        }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "BackPlaysController.cs",
                    FuncCode = "Delete(string id)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "删除(彩种信息).出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }

            return this.Json(new
            JsonResultModel
            {
                ResultCode = "-100",
                ResultMessage = string.Format("删除失败!")
            }, JsonRequestBehavior.AllowGet);
           
        }
        /// <summary>
        /// 编辑保存
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public ActionResult EditSave(PlaysShowsViewModel viewModel)
        {
            //验证...
            List<string> errMessageList = this.ValidateData(viewModel);
            if (errMessageList != null && errMessageList.Count > 0)
            {
                ErrorViewModel errorViewModel = new ErrorViewModel()
                {
                    InfoList = errMessageList
                };
                return this.ErrorResult(errorViewModel);
            }
            try
            {
                if (viewModel != null && viewModel.PlaysShows != null)
                {
                    if (viewModel.PlaysShows.Id <= 0)
                    {
                        viewModel.PlaysShows.CreateTime = DateTime.Now;
                        string playname = Request.Form["PlaysShows.PlayName"];
                        if (playname == "请选择")
                        {
                            playname = "";
                        }
                        viewModel.PlaysShows.PlayName = playname;
                     
                        PlaysBLL.Add(viewModel.PlaysShows);
                    }
                    else
                    {
                       PlaysShowsModel model=  PlaysBLL.GetInfo(viewModel.PlaysShows.Id);
                       model.LotteryID = viewModel.PlaysShows.LotteryID;
                       model.PlayName = viewModel.PlaysShows.PlayName;
                       model.PlayNo = viewModel.PlaysShows.PlayNo;
                       model.WinLotteryNumber = viewModel.PlaysShows.WinLotteryNumber;
                       model.StartTime = viewModel.PlaysShows.StartTime;
                       model.EndTime = viewModel.PlaysShows.EndTime;
                       model.PlayState = viewModel.PlaysShows.PlayState;
                       model.StateUpdatetime = DateTime.Now;
                       PlaysBLL.Update(model);
                    }
                    return this.Json(new
                    {
                        result = "100",
                        html = string.Format("{0}", "保存成功!")
                    });
                }
            }
            catch (Exception ex)
            {
                
               LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "BackPlaysController.cs",
                    FuncCode = "EditSave(PlaysShowsViewModel viewModel)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "编辑保存model(彩种).出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }

            return this.Json(new
            {
                result = "-100",
                title = "警告信息",
                html = string.Format("请尝试刷新页面或联系管理员.")
            });
           
        }
        /// <summary>
        /// 验证 相关...
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        private List<string> ValidateData(PlaysShowsViewModel viewModel)
        {

            List<string> errMessageList = new List<string>();
            if (viewModel.PlaysShows.LotteryID < 1)
            {
                errMessageList.Add("请选择彩种类型!");
            }
            if (string.IsNullOrEmpty(viewModel.PlaysShows.PlayNo))
            {
                errMessageList.Add("彩种期号不能为空!");
            }
            if (viewModel.PlaysShows.StartTime >= viewModel.PlaysShows.EndTime)
            {
                errMessageList.Add("时间输入错误!,结束时间要大于开始时间!");
            }
            //int lotteryId = viewModel.PlaysShows.LotteryID;
            //string winNo = viewModel.PlaysShows.WinLotteryNumber;

            //if (winNo!= "")
            //{

            //    switch (lotteryId)
            //    {

            //        case 2: //22选5
            //            if (!FilteGeshi(2, winNo))
            //            {
            //                errMessageList.Add("22选5彩种号码格式输入有误! 例：(01 02 03 04 05)");
            //            }
            //            break;
            //        case 3: // 七星彩
            //            if (!FilteGeshi(3, winNo))
            //            {
            //                errMessageList.Add("七星彩彩种号码格式输入有误! 例：(01 02 03 04 05 06 07)");
            //            }
            //            break;
            //        case 5: //双色球
            //            if (!FilteGeshi(5, winNo))
            //            {
            //                errMessageList.Add("双色球彩种号码格式输入有误! 例：(01 02 03 04 05 06 07)");
            //            }
            //            break;
            //        case 6: //3D
            //            if (!FilteGeshi(6, winNo))
            //            {
            //                errMessageList.Add("3D彩种号码格式输入有误! 例：(1 2 3)");
            //            }
            //            break;
            //        case 13: //七乐彩
            //            if (!FilteGeshi(13, winNo))
            //            {
            //                errMessageList.Add("七乐彩彩种号码格式输入有误! 例：(01 02 03 04 05 06 07)");
            //            }
            //            break;
            //        case 39: //大乐透
            //            if (!FilteGeshi(39, winNo))
            //            {
            //                errMessageList.Add("大乐透彩种号码格式输入有误! 例：(01 02 03 04 05 06 07)");
            //            }
            //            break;
            //        case 63: //排列三
            //            if (!FilteGeshi(63, winNo))
            //            {
            //                errMessageList.Add("排列三彩种号码格式输入有误! 例：(1 2 3)");
            //            }
            //            break;
            //        case 64: //排列五
            //            if (!FilteGeshi(63, winNo))
            //            {
            //                errMessageList.Add("排列五彩种号码格式输入有误! 例：(1 2 3 4 5)");
            //            }
            //            break;

            //    }

            //}

            return errMessageList;
        }

        public bool FilteGeshi(int lotteryId, string winNo)
        {
            bool bol = true;
                switch (lotteryId)
                {

                    case 2: //22选5
                        string[] str = winNo.Trim().Split(' ');
                        if (str.Length != 5)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str)
                            {
                                if (sr.Length != 2)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }

                        break;
                    case 3: // 七星彩
                        string[] str2 = winNo.Trim().Split(' ');
                        if (str2 != null)
                        {
                            if (str2.Length != 7)
                            {
                                bol = false;
                                break;
                            }
                            else
                            {
                                foreach (string sr in str2)
                                {
                                    if (sr.Length != 2)
                                    {
                                        bol = false;
                                        break;
                                    }
                                }
                            }
                        }



                        break;
                    case 5: //双色球

                        string[] str3 = winNo.Trim().Split(' ');

                        if (str3.Length != 7)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str3)
                            {
                                if (sr.Length != 2)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }

                        break;
                    case 6: //3D
                        string[] str4 = winNo.Split(' ');
                        if (str4.Length != 3)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str4)
                            {
                                if (sr.Length != 1)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }

                        break;
                    case 13: //七乐彩
                        string[] str5 = winNo.Trim().Split(' ');
                        if (str5.Length != 7)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str5)
                            {
                                if (sr.Length != 2)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }

                        break;
                    case 39: //大乐透
                        string[] str6 = winNo.Trim().Split(' ');
                        if (str6.Length != 7)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str6)
                            {
                                if (sr.Length != 2)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }
                        break;
                    case 63: //排列三
                        string[] str7 = winNo.Trim().Split(' ');
                        if (str7.Length != 3)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str7)
                            {
                                if (sr.Length != 1)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }

                        break;
                    case 64: //排列五
                        string[] str8 = winNo.Trim().Split(' ');
                        if (str8.Length != 5)
                        {
                            bol = false;
                        }
                        else
                        {
                            foreach (string sr in str8)
                            {
                                if (sr.Length != 1)
                                {
                                    bol = false;
                                    break;
                                }
                            }
                        }
                        break;

                }

            return bol;
        }
    }
}
